<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学习vue</title>
    <script src="../js/vue.js"></script>
    <style>
        *{
            padding: 12px;
        }
    </style>
</head>
<body>
    <div id="root">
        <!-- 
            如果写成index会有输入框在添加后错位。
            因为虚拟dom的对比算法是依赖于key。
         -->
        <!-- <div v-for="(p,index) in persons" :key="index"> -->
        <div v-for="p in persons" :key="p.id">
            {{p.name}} -- {{p.age}}
            <input type="text">
        </div>
        <button @click.once="add">加</button>
    </div>
    <script>
        Vue.config.productionTip = false
        new Vue({
            el:'#root',
            data:{
                name:'zzz1',
                persons:[
                    {id:'001',name:"f1",age:20},
                    {id:'002',name:"f2",age:20},
                    {id:'003',name:"f3",age:20},
                ],
            },
            computed:{
            },
            methods:{
                add(){
                    const p = {id:'004',name:"f4",age:40}
                    //加到第一个元素。
                    this.persons.unshift(p)
                }
            },
            watch:{
            },
        })
    </script>    
</body>
</html>